/*
 * This packet is the response from the server to the client following a move
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class MoveResponsePacket extends Packet {

    /*Move response constants, cannot us an enum because we need to allow 
     * game specific messages*/
    public static short MOVE_RESPONSE_VALID_MOVE = 0;
    public static short MOVE_RESPONSE_ILLEGAL_MOVE = 1;
    public static short MOVE_RESPONSE_INVALID_BOARD = 2;
    public static short MOVE_RESPONSE_MIN_RESERVED = 3;
    public static short MOVE_RESPONSE_MAX_RESERVED  = 127;
    public static short MOVE_RESPONSE_MIN_GAME_CODE = 128;
    public static short MOVE_RESPONSE_MAX_GAME_CODE = 32767;

    
    private short response_code;
    
    public MoveResponsePacket( short response_code ) throws PacketException {
        
        if ( response_code >= MOVE_RESPONSE_MIN_RESERVED &&
             response_code <= MOVE_RESPONSE_MAX_RESERVED ) {
            throw new PacketException( "Invalid use of reserved codes " + 
                                       response_code );
        }

        this.response_code = response_code;
    
        /*Set up the header*/
        message_type = MSG_MOVE_RESPONSE;
        game_name = 0;
        game_version = 0;
        data_area_size = 4; /*only two shorts in this packet*/
    }
    
    public MoveResponsePacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        response_code = buf.getShort();
    }
    
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        buf.putShort( response_code );
        buf.putShort( (short)0 );
        
        return buf.array();
    }
    
    public String toString() {
        return super.toString() + "\nresponse_code = " + response_code;
    }

    public short get_response_code() { return response_code; }
}
